package com.smday.queue;

import java.util.Scanner;

/**
 * @author Summerday
 */
public class ArrayQueueDemo {

    public static void main(String[] args) {

        //创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);

        char key;
        Scanner scanner = new Scanner(System.in);

        boolean loop = true;

        while(loop){

            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):从队列查看头数据");
            key = scanner.next().charAt(0);

            switch (key){

                case 's':
                    arrayQueue.showQueue();
                    break;

                case 'a':
                    System.out.println("输入一个数");
                    int i = scanner.nextInt();
                    arrayQueue.addQueue(i);
                    break;
                case 'g':
                    try{
                        int res = arrayQueue.getQueue();
                        System.out.println(res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = arrayQueue.headQueue();
                        System.out.println(head);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;

                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }


        }
        System.out.println("程序退出!");


    }


}

//编写一个类,使用数组模拟队列

class ArrayQueue{

    //队列最大容量
    private int maxSize;

    //指向队列头
    private int front;

    //指向队列尾
    private int rear;

    //用于存放数据
    private int[] arr;

    //创建队列的构造器
    public ArrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        //指向队列头部,分析出front是指向队列头的前一位置
        front = -1;
        //指向队列尾,指向队列尾的数据,即队列的最后一个数据
        rear = -1;
    }

    //判断队列是否满
    public boolean isFull(){
        return rear == maxSize-1;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }
    //添加数据到队列
    public void addQueue(int n){
        //判断队列是否满
        if(isFull()){
            System.out.println("队列满!");
            return;
        }
        //rear后移
        rear++;
        arr[rear] = n;
    }

    //获取队列的数据,出队列
    public int getQueue(){
        //判断队列是否为空
        if(isEmpty()){
            throw new RuntimeException("队列为空!");
        }
        front++;
        return arr[front];
    }
    //显示数据
    public void showQueue(){
        if(isEmpty()){
            System.out.println("数列为空");
            return;
        }
        for(int i = 0;i<arr.length;i++){
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }

    //显示队列的头数据

    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空!");
        }
        return arr[front+1];
    }

}
